﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using UIControls.Core;
using BO;
namespace UIControls
{
    public class CsComboBox : Core.LabelCell, IUIControl
    {
        public ComboBox ComboBox;
        public CsComboBox()
        {
            InitializeComponent();
            ComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        private void InitializeComponent()
        {
            this.ComboBox = new System.Windows.Forms.ComboBox();
            this.Controls.Add(this.ComboBox);
            this.SuspendLayout();
            // 
            // ComboBox
            // 
            this.ComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
          )));
            this.ComboBox.FormattingEnabled = true;
            this.ComboBox.Location = new System.Drawing.Point(0, 5);
            this.ComboBox.Name = "ComboBox";
    
            this.ResumeLayout(false);

        }

        public string LabelText
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(250, 30);
            }
        }
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, width, DefaultSize.Height, specified);
        }
        private int _LabelWidth=100;
        public int LabelWidth
        {
            get { return _LabelWidth; }
            set
            {
                _LabelWidth = value;
            }
        }
    
        protected override void OnLayout(LayoutEventArgs levent)
        {
            ComboBox.Location = new Point(this.LabelWidth, 5);

            ComboBox.Width=this.Width - this.LabelWidth;
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
      
            e.Graphics.DrawString(this.LabelText, this.Font, new SolidBrush(GlobalUIVar.gLabelColor), new Point(e.ClipRectangle.Left + 5,  e.ClipRectangle.Top + 10));
       
        }
        public void BindTo(object dataObj, string propertyName)
        {
            var t = dataObj.GetType();
            var p=t.GetProperty(propertyName);
            if (p == null)
            {
                throw new ArgumentException("没有该属性", propertyName);
            }
            else
            {
                if (p.PropertyType.IsEnum)
                {
                    var ds =  DescriptionHandler.ToList(p.PropertyType);
                    this.ComboBox.DataSource = ds;
                    this.ComboBox.ValueMember = "Key";
                    this.ComboBox.DisplayMember = "Value";
                }
                this.ComboBox.DataBindings.Add("Text", dataObj, propertyName);
            }

        }
      
    }
}
